package com.jprocms.module.cms.controller.directive;

import com.jpro.framework.common.pojo.PageResult;
import com.jprocms.module.cms.controller.admin.comment.vo.CommentListReqVO;
import com.jprocms.module.cms.dal.dataobject.comment.CommentDO;
import com.jprocms.module.cms.enums.EnumCommentStatus;
import com.jprocms.module.cms.service.comment.CommentService;
import com.jprocms.module.cms.util.Freemarkers;
import com.jprocms.module.cms.util.FrontContextUtil;
import com.jprocms.module.cms.web.Directives;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.jprocms.module.cms.web.Directives.getLong;


/**
 * 评论列表 标签
 *
 * @author jprocms
 */
public class CommentListDirective implements TemplateDirectiveModel {
    /**
     * 内容ID。Long。
     */
    public static final String CONTENT_ID = "contentId";

    public static void assemble(CommentListReqVO args, Map<String, ?> params, Long defaultSiteId) {
        args.setSiteId(defaultSiteId);
        args.setStatus(EnumCommentStatus.CHECK_PASS.getStatus());
        Optional.ofNullable(getLong(params, CONTENT_ID)).ifPresent(args::setContentId);
    }

    protected void doExecute(Environment env, Map<String, TemplateModel> params, TemplateModel[] loopVars,
                             TemplateDirectiveBody body, boolean isPage) throws TemplateException, IOException {
        Freemarkers.requireLoopVars(loopVars);
        Freemarkers.requireBody(body);

        CommentListReqVO args = new CommentListReqVO();
        assemble(args, params, FrontContextUtil.getSiteId(env));
        if (isPage) {
            int page = Directives.getPage(params, env);
            int pageSize = Directives.getPageSize(params, env);
            args.setPageSize(pageSize);
            args.setPageNo(page);
            PageResult<CommentDO> commentPage = service.getCommentPage(args);
            Directives.setTotalPages(commentPage.getTotalPages());
            loopVars[0] = env.getObjectWrapper().wrap(commentPage);
        } else {
            int offset = Directives.getOffset(params);
            int limit = Directives.getCount(params);
            args.setCount(limit);
            args.setOffset(offset);
            List<CommentDO> list = service.getCommentList(args);
            loopVars[0] = env.getObjectWrapper().wrap(list);
        }
        body.render(env.getOut());
    }

    @SuppressWarnings("unchecked")
    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
            throws TemplateException, IOException {
        doExecute(env, params, loopVars, body, false);
    }

    private final CommentService service;

    public CommentListDirective(CommentService service) {
        this.service = service;
    }
}
